package com.cqs.leetcode.bit;

import com.google.common.collect.Lists;

import java.util.List;

public class SingleNumberII137 {

    public int singleNumber(int[] nums) {
        int result = 0;
        for (int i = 0; i < 32; i++) {
            int sum = 0;
            for (int j = 0; j < nums.length; j++) {
                //二进制第31-i位的值求和
                sum += (nums[j] >> i) & 1;
            }
            //出现三次的 一定可以被三整除
            result += (1 << i) * (sum % 3);
        }
        return result;
    }

    public static void main(String[] args) {


        SingleNumberII137 sg = new SingleNumberII137();
        List<int[]> tests = Lists.newArrayList(new int[]{5,5,5,7,8,8,10,8,10,10});
        tests.forEach(it->{
            int i = sg.singleNumber(it);
            System.out.println(it+"\t" + i);
        });
    }
}
